import tkinter as tk
from tkinter import messagebox

class GobangGame:
    def __init__(self, root):
        # 设置窗口标题
        self.root = root
        self.root.title("五子棋")
        
        # 棋盘大小设置
        self.size = 15  # 15x15的棋盘
        self.cell_size = 40  # 每个格子的大小
        self.margin = 20  # 边距
        
        # 计算窗口大小
        self.window_size = self.margin * 2 + self.cell_size * (self.size - 1)
        
        # 创建画布
        self.canvas = tk.Canvas(
            root, 
            width=self.window_size, 
            height=self.window_size,
            bg="#E8C083"  # 棋盘颜色
        )
        self.canvas.pack()
        
        # 创建重新开始按钮
        self.restart_btn = tk.Button(root, text="重新开始", command=self.restart_game)
        self.restart_btn.pack(pady=10)
        
        # 初始化游戏数据
        self.board = [[0 for _ in range(self.size)] for _ in range(self.size)]  # 0:空, 1:黑, 2:白
        self.current_player = 1  # 1表示黑棋先行
        self.game_over = False
        
        # 绑定鼠标点击事件
        self.canvas.bind("<Button-1>", self.on_click)
        
        # 绘制棋盘
        self.draw_board()
    
    def draw_board(self):
        """绘制棋盘网格"""
        # 绘制横线和竖线
        for i in range(self.size):
            # 横线
            self.canvas.create_line(
                self.margin, 
                self.margin + i * self.cell_size,
                self.window_size - self.margin,
                self.margin + i * self.cell_size,
                width=2
            )
            # 竖线
            self.canvas.create_line(
                self.margin + i * self.cell_size,
                self.margin,
                self.margin + i * self.cell_size,
                self.window_size - self.margin,
                width=2
            )
        
        # 绘制天元和星位
        star_points = [(3, 3), (3, 11), (7, 7), (11, 3), (11, 11)]
        for (x, y) in star_points:
            self.canvas.create_oval(
                self.margin + x * self.cell_size - 5,
                self.margin + y * self.cell_size - 5,
                self.margin + x * self.cell_size + 5,
                self.margin + y * self.cell_size + 5,
                fill="black"
            )
    
    def draw_stone(self, x, y, color):
        """绘制棋子"""
        center_x = self.margin + x * self.cell_size
        center_y = self.margin + y * self.cell_size
        radius = self.cell_size // 2 - 2
        
        self.canvas.create_oval(
            center_x - radius,
            center_y - radius,
            center_x + radius,
            center_y + radius,
            fill=color,
            outline="black"
        )
    
    def on_click(self, event):
        """处理鼠标点击事件"""
        if self.game_over:
            return
        
        # 计算点击位置对应的棋盘坐标
        x = round((event.x - self.margin) / self.cell_size)
        y = round((event.y - self.margin) / self.cell_size)
        
        # 检查坐标是否在有效范围内
        if 0 <= x < self.size and 0 <= y < self.size:
            # 检查该位置是否已有棋子
            if self.board[y][x] == 0:
                # 记录落子
                self.board[y][x] = self.current_player
                
                # 绘制棋子
                color = "black" if self.current_player == 1 else "white"
                self.draw_stone(x, y, color)
                
                # 检查是否获胜
                if self.check_win(x, y):
                    winner = "黑棋" if self.current_player == 1 else "白棋"
                    messagebox.showinfo("游戏结束", f"{winner}获胜！")
                    self.game_over = True
                    return
                
                # 切换玩家
                self.current_player = 2 if self.current_player == 1 else 1
    
    def check_win(self, x, y):
        """检查是否有一方获胜"""
        directions = [
            [(1, 0), (-1, 0)],  # 水平方向
            [(0, 1), (0, -1)],  # 垂直方向
            [(1, 1), (-1, -1)], # 对角线方向
            [(1, -1), (-1, 1)]  # 反对角线方向
        ]
        
        player = self.board[y][x]
        
        for dir_pair in directions:
            count = 1  # 记录连续棋子数量，当前位置已有一个
            
            # 检查每个方向的两个方向
            for dx, dy in dir_pair:
                nx, ny = x + dx, y + dy
                while 0 <= nx < self.size and 0 <= ny < self.size and self.board[ny][nx] == player:
                    count += 1
                    nx += dx
                    ny += dy
            
            # 如果有五个或更多连续棋子，获胜
            if count >= 5:
                return True
        
        return False
    
    def restart_game(self):
        """重新开始游戏"""
        # 清空画布
        self.canvas.delete("all")
        
        # 重置游戏数据
        self.board = [[0 for _ in range(self.size)] for _ in range(self.size)]
        self.current_player = 1
        self.game_over = False
        
        # 重新绘制棋盘
        self.draw_board()

if __name__ == "__main__":
    root = tk.Tk()
    game = GobangGame(root)
    root.mainloop()